/**
 * @file snake.c
 * @author  林彦年(1216840923@qq.com)
 * @brief 一个蛇结点为12*12的像素点
 * @version 0.1
 * @date 2022-08-15
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "game.h"


//游戏结束标志
int gameOver = 0;
int exitgame = 0;

//TCP套接字
int socketFd;
//UDP套接字
int udp_socketFd;

//TCP地址、端口号
struct sockaddr_in serverAddr;
//UDP地址、端口号
struct sockaddr_in udp_serverAddr;

//地图图片数据指针
int (*pic_arr)[12*12];


/**
 * @brief 初始化游戏数据
 * 
 */
void init_game()
{
    int width,height;
    int bmp_tmp[12*12] = {0};
    char buf[800*480] = {0};

    //获取游戏中地图、蛇身、果实图片数据
    getBmpData("./doc/pic/headUp.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[snake_head_up],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    memcpy(pic_arr[snake_head_down],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_head_left],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_head_right],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/bodyColum.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[snake_body],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/tailUpSecond.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[snake_tail_second_up],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    memcpy(pic_arr[snake_tail_second_down],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_tail_second_left],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_tail_second_right],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/tailUp.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[snake_tail_up],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    squareBmpRotate(bmp_tmp,RO_LEFT,12);
    memcpy(pic_arr[snake_tail_down],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_tail_left],bmp_tmp,sizeof(bmp_tmp));

    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    squareBmpRotate(bmp_tmp,RO_RIGHT,12);
    memcpy(pic_arr[snake_tail_right],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/food.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[food],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/mapColorOne.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[mapColorOne],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/mapColorTwo.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[mapColorTwo],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/mapColorThree.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[mapColorThree],bmp_tmp,sizeof(bmp_tmp));

    getBmpData("./doc/pic/mapColorFour.bmp",buf,&width,&height);
    picProcess(buf,bmp_tmp,width,height);
    memcpy(pic_arr[mapColorFour],bmp_tmp,sizeof(bmp_tmp));

}


int main()
{
    //初始化
    initLog();//调试函数初始化
    init_game(); //初始化游戏数据(获取图片数据，保存在pic_arr[]数组当中)
    int x,y;

    int arr[19][144];//共19个图片，每个图片有144个像素点。
    pic_arr = arr;
    //pic_arr = malloc(19*144);

    //显示菜单图片，并判断是否点击开始游戏
    showBmp("./doc/pic/snakeInterface.bmp",0,0,DISPLAY_ALL);
    do{
        getLoca(NULL,&x,&y,NULL,NULL);
    }while(touch_simgle_region(x,y,240,330,560,420)!=BETOUCH);
    
    //显示正式的游戏图片
    showBmp("./doc/pic/snake.bmp",0,0,DISPLAY_ALL);
    
    //与服务器建立TCP连接
    int ret;
    socketFd = socket(AF_INET,SOCK_STREAM,0);

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    serverAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

    ret = connect(socketFd,(struct sockaddr*)&serverAddr,sizeof(struct sockaddr_in));
    if(ret == -1){
        perror("connect error");
        return -1;        
    }

    //与服务器建立UDP连接
    int len = sizeof(struct sockaddr);
    char buf[10] = {0};
    udp_socketFd = socket(AF_INET,SOCK_DGRAM,0);

    udp_serverAddr.sin_family = AF_INET;
    udp_serverAddr.sin_port = htons(SERVER_PORT);
    udp_serverAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

    //用UDP发送数据告知服务器该客户端IP和端口号（如何告知？？？）
    sendto(udp_socketFd,buf,sizeof(buf),0,(struct sockaddr*)&serverAddr,len);
    //recvfrom(udp_socketFd,buf,sizeof(buf),0,(struct sockaddr*)&serverAddr,&len);

    //创造网络通信读线程和写线程
    pthread_t recv_thread,send_thread;
    pthread_create(&recv_thread,NULL,tcp_recv,&socketFd);
    pthread_create(&send_thread,NULL,tcp_send,&socketFd);

    //向服务器发送开始游戏信号
    tcp_client_data send_data;
    send_data.command = START_GAME;
    send(socketFd,&send_data,sizeof(tcp_client_data), 0);

    //等待该客户端游戏结束
    pthread_join(send_thread,NULL);
    pthread_join(recv_thread,NULL);
    
    free(pic_arr);
    return 0;
}